<?php
//module xin nghi cua nhan vien, duyet don xin nghi
class Emp_Off extends Public_Controller{

	private $page_size = 10;
	private $is_boss = false;
	private $boss_name = NULL;
	function __construct(){
		parent::__construct();
		$this->boss_name =  $this->current_user->group;
		if(substr($this->boss_name,0,4)=='boss')
			$this->is_boss = true;//la cap tren ko phai employee
		$menu_left = "";
		if(!$this->input->is_ajax_request()){
			$menu_left = $this->menu_left();
			$this->template->set('loading',true);
		}
		
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11'){
			//$this->template->set_layout('body.html');
		}
		$this->template
			->set_layout('left_body_module.html')
			->append_css('left_body.css')
			->set('menu_left',$menu_left);
		$this->load->model('off_m');
		$this->load->helper('kunkun_page');
		//print_r($this->current_user);
		//$this->load->language($this->module.'/'.$this->controller,$this->lang_current);
	}
	private function menu_left(){
		//menu left
		$this->load->config('config');
		$model = $this->config->item('menu_left');
		//role : neu la user thi ko lay role
		if($this->current_user->group == $this->name_employee){
			foreach($model as $key =>$value){
				if(isset($value['role'])){
					unset($model[$key]);
				}
			}
		}
		//neu giam doc hay pho giam doc dang nhap thi unset menu 'employee'
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11'){
			foreach($model as $key =>$value){
				if(isset($value['employee'])){
					unset($model[$key]);
				}
			}
		}
		
		$day_off = $this->current_profile->allows_1;
		if(isset($model[3]['data']))
			$model[3]['data'] = " : ".$day_off;
		//print_r($model);exit;
		//echo $day_off;exit;
		return $this->template
			->set_layout(NULL)
			->set('model',$model)->build('block/menu.html');
	}
	public function	index(){
		$id = $this->current_user->id;		
		/*$array = $this->input->post();
		if(isset($array)){
			$object = (object)$array; // Only for print array
			$this->xinnghi_ajax($object);
		}*/
		
		$this->template
			->set_breadcrumb(lang('emp_off:off'))
			->append_css('kendo/examples-offline.css')
			->append_css('kendo/kendo.common.min.css')
			->append_css('kendo/kendo.default.min.css')
			->append_css('emp_off.css')		
			->append_js('kendo/kendo.web.min.js')			
			->append_js('kendo/console.js')	
			//time
			->append_css('ui/themes/redmond/jquery-ui.min.css')
			->append_js('ui/jquery-ui.min.js')
			->append_css('business/jquery.ui.timepicker.css')
			->append_js('business/jquery.ui.timepicker.js')
			->set('image1',$this->template->get_theme_path().'img/kendo/dropdownlist/cap.png')			
			->build($this->controller.'/'.$this->router->method);
	}
	//POST jax
	function xinnghi_ajax(){
		$id = $this->current_user->id;
		$profile = $this->current_profile;
		$room_id = $profile->room_id;
		$object = array();
		$s = false;
		//return print_r($object);exit;
		$array = $this->input->post();
		if(isset($array)){
			$object = $array; // Only for print array
			$object['user_id'] = $id;
			$object['month'] = date('m');
			$object['room_id'] = $room_id;
			//str bat dau
			$s1 = strtotime($object['time_start']);
			$s2 = strtotime($object['time_end']);
			if(!empty($object['timepicker_start'])){
				$s1 = $object['time_start'] .' '. $object['timepicker_start'] ;
				$s1 = strtotime($s1);
				//echo $s1;exit;
			}
			if(!empty($object['timepicker_end'])){
				$s2 = $object['time_end'] .' '. $object['timepicker_end'] ;
				$s2 = strtotime($s2);
			}
			//echo $s1;exit;
			$object['time_start'] = $s1;
			$object['time_end'] = $s2;
			//echo $object['time_start'];exit;
			$object['salary'] = 0;
			$object['status'] = 0;
			$object['value'] = isset($object['value']) ? $object['value'] : 0;
			$object['daycreated'] = date('m/d/Y',$object['time_start']);
			$object['daycreated'] = strtotime($object['daycreated']);
			$object['position_id'] = $this->current_user->group_id;
			if($this->is_boss){
				$object['important'] = 1;//truong phong xin nghi
			}
			unset($object['timepicker_start']);
			unset($object['timepicker_end']);
			//print_r($object);exit;
			$this->off_m->add($object);
			
		}
		echo json_encode($object);
	}
	
	public function waiting(){
		$this->load->helper('admin_theme');
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$base_where = array(
			'room_id'=> $this->current_profile->room_id,
			'current_user' => $this->current_user->id,
			//'in_month'	=> true
		);
		//giam doc/ pho giam doc xem toan bo cty nhung don xin nghi nao important = 1
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11' ){
			$base_where = array(
			'room_id'=> NULL,
			//'status'	=> 0,
			'important' => 1,
			'view_room' => true
			);
		}
		$base_where = $this->input->get('f_page') ? $base_where + array('limit' => $this->input->get('f_page')) : $base_where;
		$base_where = $this->input->get('type_off') ? $base_where + array('type_off' => $this->input->get('type_off')) : $base_where;
		$base_where = $this->input->get('status') ? $base_where + array('status' => $this->input->get('status')) : $base_where;
		$base_where = $this->input->get('user_id') ? $base_where + array('user_id' => $this->input->get('user_id')) : $base_where;
		$base_where = $this->input->get('month') ? $base_where + array('month' => $this->input->get('month')) : $base_where;
		$base_where = $this->input->get('year') ? $base_where + array('year' => $this->input->get('year')) : $base_where;
		if($this->input->get('status') == 0){
			$base_where['status'] = 0;
		}
		if($this->input->get('type_off') == '0'){
			$base_where['type_off'] = 0;
		}
		//print_r($base_where);
		$model = $this->off_m->get_many_by($base_where,$page_size = 1000);//result | total_row
		$page_index =  $this->input->get('f_page') ?  $this->input->get('f_page') : 0;
		$status = $this->input->get('status') ? true : false;
		//echo $model->total_row;exit;
		$total_page = total_page($model->total_row,$this->page_size);	
		//type_off
		$type_off = $this->off_m->type_off_data;
		$this->load->model('emp_profile/profile_m');
		$employees = $this->profile_m->get_by_room($this->current_profile->room_id);
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11' ){
			$employees = $this->profile_m->get_all();
		}
		$employees_select = array_for_select($employees,'id','display_name');
		//month
		$this->load->model('number_m');
		$month_select = $this->number_m->month_select();
		//year
		$year_select = $this->number_m->year_select($this->year_start,date("Y"));
		$this->template
			->set_breadcrumb(lang('emp_off:list_off'))
			->append_js('ui/jquery-ui.min.js')
			->append_css('ui/jquery-ui.min.css')
			->append_css('table2.css')
			->append_js('admin/scripts.js')
			->append_js('business/site.js')
			->append_js('admin/admin.js')
			->append_js('chosen/chosen.jquery.min.js')
			->append_css('chosen/chosen.css')
			->append_js('filter.js')
			->set(array(
				'model'=>$model->result,
				'total_page' => $total_page,
				'type_off'	=>$type_off,
				'page_index' => $page_index,
				'employees_select' => $employees_select,
				'status'	=> $status,
				'month_select' => $month_select,
				'year_select' => $year_select
			));
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/list') : $this->template->build($this->controller.'/list');
	}
	
	//lay so don xin nghi cho duyet
	
	private function base_where_cease(){
		//truong phong xem phong minh
		$base_where = array(
			'room_id'=> $this->current_profile->room_id,
			'current_user' => $this->current_user->id,
			'status'	=> 0,
			'view_room' => false,
			//khong xem status = 0 , boss_status = 2
			'boss_status' => '!=2'
		);
		//giam doc/ pho giam doc xem toan bo cty nhung don xin nghi nao important = 1
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11' ){
			$base_where = array(
			'room_id'=> NULL,
			'status'	=> 0,
			'important' => 1,
			'boss_status' => 2,
			'view_room' => true
			);
		}
		return $base_where;
	}
	public function count_cease(){
		$base_where = $this->base_where_cease();
		$model = $this->off_m->get_many_by($base_where,$page_size = 1000);//result | total_row
		$total = count($model->result);
		$data = array('title'=>lang('emp_off:off_waiting_title'),'total'=>$total);
		echo json_encode($data);
	}
	public function cease(){
		//$this->time_zone();
		$base_where = $this->base_where_cease();
		$model = $this->off_m->get_many_by($base_where,$page_size = 1000);//result | total_row
		//echo $this->db->last_query();exit;
		$this->load->library('kunkuncache');
		$file_cache = 'emp_profile/profile_';//file cache 1 profile
		foreach($model->result as $item){
			$o = 480;
			$item->day = round($item->value/$o,PHP_ROUND_HALF_DOWN);
			$item->profile = $this->kunkuncache->get($file_cache.$item->user_id); 
		}
		//print_r($model->result);exit;//123456
		$this->template
			->set_breadcrumb(lang('emp_off:off_waiting_title'))
			->set('model',$model->result)
			->set('view_room',$base_where['view_room'])
			->append_css('table2.css')
			->build($this->controller.'/cease');
	}
	
	public function status($id,$status,$method){
		//user 
		$off = $this->off_m->get($id);//123456
		$this->load->library('kunkuncache');
		$file_cache = 'emp_profile/profile_';//file cache 1 profile
		$profile = $this->kunkuncache->get($file_cache.$off->user_id); 
		$o = 480;
		/*$day = round($off->value/$o,PHP_ROUND_HALF_DOWN);
		if($profile->allows_1 > $day){ // ngay phep phai du
			//update profile 
			
			$profile->allows_1 = $profile->allows_1 - $day;
			$this->load->model('emp_profile/profile_m');
			$p = $this->profile_m->update($off->user_id,array('allows_1'=>$profile->allows_1));
			$this->kunkuncache->write($profile,$file_cache.$off->user_id);
		}*/
		
		$salary = NULL;
		
		/*if($profile->allows_1 < 1)
			$salary = true;*/
			
		$this->off_m->status($id,$status,$salary);
		redirect('emp_off/'.$method);
	}
	
	public function view(){
		$this->time_zone();
		$this->load->helper('admin_theme');
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$base_where = array(
			'room_id'=> $this->current_profile->room_id,
			//'current_user' => $this->current_user->id,
			'user_id' => $this->current_user->id,
			//'in_month'	=> true
		);
		$base_where = $this->input->get('f_page') ? $base_where + array('limit' => $this->input->get('f_page')) : $base_where;
		$base_where = $this->input->get('type_off') ? $base_where + array('type_off' => $this->input->get('type_off')) : $base_where;
		$base_where = $this->input->get('status') ? $base_where + array('status' => $this->input->get('status')) : $base_where;
		$base_where = $this->input->get('month') ? $base_where + array('month' => $this->input->get('month')) : $base_where;
		$base_where = $this->input->get('year') ? $base_where + array('year' => $this->input->get('year')) : $base_where;
		if($this->input->get('status') == 0){
			$base_where['status'] = 0;
		}
		if($this->input->get('type_off') == '0'){
			$base_where['type_off'] = 0;
		}
		//print_r($base_where);
		$model = $this->off_m->get_many_by($base_where,$page_size = 1000);//result | total_row
		$page_index =  $this->input->get('f_page') ?  $this->input->get('f_page') : 0;
		$status = $this->input->get('status') ? true : false;
		//echo $model->total_row;exit;
		$total_page = total_page($model->total_row,$this->page_size);	
		//type_off
		$type_off = $this->off_m->type_off_data;
		$this->load->model('emp_profile/profile_m');
		$employees = $this->profile_m->get_by_room($this->current_profile->room_id);
		if($this->boss_name == 'boss1' || $this->boss_name == 'boss11' ){
			$employees = $this->profile_m->get_all();
		}
		$employees_select = array_for_select($employees,'id','display_name');
		//month
		$this->load->model('number_m');
		$month_select = $this->number_m->month_select();
		//year
		$year_select = $this->number_m->year_select($this->year_start,date("Y"));
		$this->template
			->set_breadcrumb(lang('emp_off:list_off').' - '.$this->current_profile->display_name)//123456
			->append_js('ui/jquery-ui.min.js')
			->append_css('ui/jquery-ui.min.css')
			->append_css('table2.css')
			->append_js('admin/scripts.js')
			->append_js('business/site.js')
			->append_js('admin/admin.js')
			->append_js('chosen/chosen.jquery.min.js')
			->append_css('chosen/chosen.css')
			->append_js('filter.js')
			->set(array(
				'model'=>$model->result,
				'total_page' => $total_page,
				'type_off'	=>$type_off,
				'page_index' => $page_index,
				'employees_select' => $employees_select,
				'status'	=> $status,
				'month_select' => $month_select,
				'year_select' => $year_select
			));
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/view') : $this->template->build($this->controller.'/view');
	}
	
	public function delete($id){
		$this->off_m->delete($id);
		redirect('emp_off/view');
	}
	
	//NGAY PHEP
	
}